package springInvoke.config;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Inject;

import org.hardy.reflex.CglibReflex;
import org.hardy.springutil.BeanManager;
import org.hardy.springutil.SpringBeanUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import feign.FeignException;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import hardy.grpc.client.Api;
import hardy.grpc.client.cloud.ApiCloud;
import hardy.grpc.client.cloud.StubIRule;
import hardy.grpc.client.cloud.StubIRule.HysimpleStubRule;
import hardy.grpc.client.cloud.StubIRule.WeightStubRule;
//import hardy.grpc.client.cloud.ApiCloud;
import springInvoke.feignface.PersonClient;
import springInvoke.feignface.RpcUtil;
import test.common.inface.OpeDatas;
import test.common.inface.OpeHumman;
//import springInvoke.message.SendService;

@Configuration
@RestController
public class InvokeConfig {
	 //===================feign===========================================
	 @Inject
	 private PersonClient personClient;
	 @Inject
	 private OpeDatas data;
	 @Inject
	 private OpeHumman human;
//	 @Inject
//	 SendService sendService;
	 @RequestMapping("/personFeign")
	 public String invokePerson(String name,int age) {
		 //此处发送了信息
		 //Message<String> msg = MessageBuilder.withPayload("我要调用你的查询person方法 name:"+name+"  age:"+age).build();
		 //sendService.sendOrder().send(msg);
//		 int r = data.plusData(1, 22);
//		 int r = ApiCloud.getProxyTarget(OpeDatas.class).plusData(1, 22);
		 int r = 0;StringBuilder s = new StringBuilder("");
		 if("data".equals(name)) r = data.plusData(age, 0);
		 if("human".equals(name)) {
			 human.createHumman();
			 human.listHumman(null).forEach(h->{
				 s.append("-").append(h.getName());
			 });
		 }
		 return personClient.person(name+s, r+age);
	 }
	 
	 @RequestMapping(value="/personsFeign")
	 public List<String> invokePersons(String[] names) {
		 return personClient.persons(names);
	 }
	  
	 @RequestMapping(value="/login")
	 public Map<String,String> login(String name,String pw) {
		 Map<String,String> map = new HashMap<>();
		 map.put("autorizer", UUID.randomUUID().toString());
		 map.put("usr", name);
		 return map;
	 }
	 
	 @Bean
	 public ResponseEntityDecoder feignDecoder() {
		 Decoder decoder = new Decoder(){

			@Override
			public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
				return JSONObject.parseObject(response.body().toString(), type);
			}
			 
		 };
		 return new ResponseEntityDecoder(decoder);
	 }
	 
	 @Bean
	 public OpeDatas opeDatas() {
		 HysimpleStubRule ws = new StubIRule.HysimpleStubRule();
		 ApiCloud.setRule(ws);
		 return ApiCloud.service(OpeDatas.class,"localhost:9991","localhost:9992");
	 }
	 
	 @Bean
	 public OpeHumman opeHumman() {
		 return ApiCloud.service(OpeHumman.class,"localhost:9991","localhost:9992");
	 }
}
